package cn.qidisoft.edu.hzjt.service.xb.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.qidisoft.core.common.code.QdfRespCode;
import cn.qidisoft.core.common.exception.dao.DBException;
import cn.qidisoft.core.common.log.LoggerAdapter;
import cn.qidisoft.core.common.log.LoggerAdapterFactory;
import cn.qidisoft.core.common.pagination.Page;
import cn.qidisoft.core.common.pagination.PaginationBean;
import cn.qidisoft.edu.hzjt.dao.xb.PartyEmploySuperviseAttMapper;
import cn.qidisoft.edu.hzjt.dao.xb.PartyEmploySuperviseMapper;
import cn.qidisoft.edu.hzjt.model.xb.PartyEmploySuperviseAttEn;
import cn.qidisoft.edu.hzjt.model.xb.PartyEmploySuperviseEn;
import cn.qidisoft.edu.hzjt.service.xb.IPartyEmploySuperviseService;
import cn.qidisoft.edu.hzjt.utils.LogType;

@Service
public class PartyEmploySuperviseServiceImpl implements IPartyEmploySuperviseService {

	protected static final LoggerAdapter LOGGER = LoggerAdapterFactory
			.getLogger(LogType.HZJT_LOGGER.val);
	
	@Autowired
	private PartyEmploySuperviseMapper mapper;
	
	@Autowired
	private PartyEmploySuperviseAttMapper mapperAtt;
	
	@Override
	public List<PartyEmploySuperviseEn> queryList(PartyEmploySuperviseEn model) {
		return mapper.queryList(model);
	}

	@Override
	public PaginationBean<PartyEmploySuperviseEn> queryPageList(PartyEmploySuperviseEn model,
			Page page) {
		try {
			int totalRecords = mapper.queryPageListcount(model);
			
			 PaginationBean<PartyEmploySuperviseEn> pageHolder = new PaginationBean<>(page, totalRecords);
	            List<PartyEmploySuperviseEn> resultList = mapper
	                    .queryPageList(model,
	                            new RowBounds((pageHolder.getCurrentPage() - 1) * pageHolder.getPageSize(),
	                                    pageHolder.getPageSize()));
	            pageHolder.setPageList(resultList);
	            return pageHolder;
		} catch (Exception e) {
			LOGGER.error("queryPageList [{}] error," + this.getClass().getName() + "=[{}]",
                    this.getClass().getName(),
                    ToStringBuilder.reflectionToString(model, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                    e);
		}
	}

	@Override
	public PartyEmploySuperviseEn queryByPK(PartyEmploySuperviseEn model) {
		return mapper.queryByPK(model);
	}

	@Override
	public int insert(PartyEmploySuperviseEn model) {
		return mapper.insert(model);
	}

	@Override
	public int update(PartyEmploySuperviseEn model) {
		mapper.update(model);
		return 0;
	}

	@Override
	public int delete(PartyEmploySuperviseEn model) {
		mapper.delete(model);
		return 0;
	}

	@Override
	public List<Map<String, Object>>  queryAllYearList() {
		return mapper.queryAllYearList();
	}

	@Override
	public PaginationBean<PartyEmploySuperviseEn> queryPageList2(PartyEmploySuperviseEn PartyEmploySuperviseEn,
			Page page) {
		try {
			int totalRecords = mapper.queryPageListcount2(PartyEmploySuperviseEn);
			
			 PaginationBean<PartyEmploySuperviseEn> pageHolder = new PaginationBean<>(page, totalRecords);
	            List<PartyEmploySuperviseEn> resultList = mapper
	                    .queryPageList2(PartyEmploySuperviseEn,
	                            new RowBounds((pageHolder.getCurrentPage() - 1) * pageHolder.getPageSize(),
	                                    pageHolder.getPageSize()));


	            List<PartyEmploySuperviseEn> lst = new ArrayList<>();
				PartyEmploySuperviseAttEn att;
				for (PartyEmploySuperviseEn PartyEmploySuperviseEn2 : resultList) {
					PartyEmploySuperviseEn en = new PartyEmploySuperviseEn();
					ConvertUtils.register(new DateConverter(null), java.util.Date.class);

					BeanUtils.copyProperties(en, PartyEmploySuperviseEn2);
					att = new PartyEmploySuperviseAttEn();
					att.setPyear(PartyEmploySuperviseEn2.getJdNf());
					List<PartyEmploySuperviseAttEn> attList = mapperAtt.queryList2(att);
					en.setjDNFYear(PartyEmploySuperviseEn2.getJdNf()+"年");
					en.setAttList(attList);

					lst.add(en);
				}

				pageHolder.setPageList(lst);
				return pageHolder;
		} catch (Exception e) {
			LOGGER.error("queryPageList [{}] error," + this.getClass().getName() + "=[{}]",
                    this.getClass().getName(),
                    ToStringBuilder.reflectionToString(PartyEmploySuperviseEn, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                    e);
		}
	}

	@Override
	public List<PartyEmploySuperviseEn> exportList(PartyEmploySuperviseEn model, Page page) {
		try {
			PaginationBean<PartyEmploySuperviseEn> pageHolder = new PaginationBean<>(page, 999999);
            List<PartyEmploySuperviseEn> resultList = mapper
                    .queryPageList2(model,
                            new RowBounds((pageHolder.getCurrentPage() - 1) * pageHolder.getPageSize(),
                                    pageHolder.getPageSize()));


            List<PartyEmploySuperviseEn> lst = new ArrayList<>();
			PartyEmploySuperviseAttEn att;
			for (PartyEmploySuperviseEn PartyEmploySuperviseEn2 : resultList) {
				PartyEmploySuperviseEn en = new PartyEmploySuperviseEn();
				ConvertUtils.register(new DateConverter(null), java.util.Date.class);

				BeanUtils.copyProperties(en, PartyEmploySuperviseEn2);
				att = new PartyEmploySuperviseAttEn();
				att.setPyear(PartyEmploySuperviseEn2.getJdNf());
				List<PartyEmploySuperviseAttEn> attList = mapperAtt.queryList2(att);
				en.setjDNFYear(PartyEmploySuperviseEn2.getJdNf()+"年");
				en.setAttList(attList);

				lst.add(en);
			}
			
			return lst;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
